import axios from 'axios'
import { Message } from 'element-ui';
import $store from '@/store/index.js';
import $router from '@/router/index.js'


const orrequest = axios.create({
	baseURL: 'http://dida100.com:8888', //默认请求域名
	timeout: 5000,//请求超时
})

orrequest.interceptors.request.use(function (config) {
	config.headers.Authorization = 'Bearer ' + localStorage.getItem("token");
	return config;
})
orrequest.interceptors.response.use(
	function (res) {
		return res
	},
	function (err) {
		console.log("错误", err);
		if (err.response && err.response.data && err.response.data.code == 1) {
			Message({
				message: err.response.data.msg || '没有权限',
				type: 'warning'
			})
			$store.dispatch('logout')
			var $route = $router.history.current;
			$router.replace('/?redirect=' + $route.fullPath)
		} else if (err.code == "ERR_BAD_REQUEST") {
			Message({
				message: '请求地址发生错误',
				type: 'warning'
			})
		}
	}
)

export default orrequest;
